package org.wen.soft.asm.handler;

import org.objectweb.asm.Type;

import java.util.ArrayList;
import java.util.List;

public class DescriptorType {

    List<String> parameterClassNameList = new ArrayList<>();
    List<String> parameterSimpleNameList = new ArrayList<>();

    String returnClassName;
    String returnSimpleName;

    public DescriptorType(String typeDescriptor) {
        if (typeDescriptor == null || typeDescriptor.isEmpty()) {
            return;
        }
        Type type = Type.getType(typeDescriptor);
        if (type.getSort() == Type.METHOD) {
            // 返回值
            Type returnType = type.getReturnType();
            returnClassName = returnType.getClassName();
            returnSimpleName = SimpleUtil.simpleName(returnClassName);
            // 参数
            for (Type argumentType : type.getArgumentTypes()) {
                String argumentClassName = argumentType.getClassName();
                parameterClassNameList.add(argumentClassName);
                parameterSimpleNameList.add(SimpleUtil.simpleName(argumentClassName));
            }
            return;
        }
        if (type.getSort() == Type.OBJECT) {
            returnClassName = type.getClassName();
            returnSimpleName = SimpleUtil.simpleName(returnClassName);
        }
    }

    public String getClassName(){
        return returnClassName;
    }

    public String getSimpleName(){
        return returnSimpleName;
    }

    public List<String> getParameterClassNames(){
        return parameterClassNameList;
    }

    public List<String> getParameterSimpleNames(){
        return parameterSimpleNameList;
    }

}
